package yl.hs.bmipfull.controllers.flow;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.flow.FlowUserTaskEntity;
import yl.hs.bmipfull.service.flow.IFlowDataService;
import yl.hs.bmipfull.service.flow.IFlowUserTaskService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.service.system.IUserService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.datastate.FlowTaskState;
import yl.hs.bmipfull.viewmodels.LoginUser;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.flow.FlowDataHandleViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 张凯
 * @apiNote 流程任务信息控制层
 * @since 2021年07月08日 16时53分56秒
 */
@Api(value = "流程任务信息Web接口", tags = "流程管理——流程任务信息操作")
@RequestMapping(value = "/api/flowUserTask")
@RestController
public class FlowUserTaskController extends ControllerBase {

    @Autowired
    IFlowUserTaskService service;

    @Autowired
    IFlowDataService flowDataService;
    @Autowired
    IUserService userService;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询流程任务信息", notes = "查询流程任务信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询流程任务信息", notes = "分页查询流程任务信息")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable(required = false) int page,
                               @ApiParam(value = "每页条数") @PathVariable(required = false) int rowNum) {
        try {
            LoginUser loginUser = HsApp.getCurrent();
            var map = query.getQuery();
            svSVC.updateUserMenuView(query);
            String userAction = "";
            if (map.get("userAction") != null) {
                userAction = map.get("userAction").toString();
                if (userAction.equals("1")) {
                    //查询我的待办事项
                    if (loginUser != null) {
                        map.put("handlerUserSN", loginUser.getUserSN());
                        map.put("taskState", FlowTaskState.UNREAD.code);
                        query.setQuery(map);
                    }
                } else if (userAction.equals("2")) {
                    //查询我的发起待办事项
                    if (loginUser != null) {
                        map.put("userSN", loginUser.getUserSN());
                        map.put("groupDataSN", 1);
                        query.setQuery(map);
                    }
                } else if (userAction.equals("3")) {
                    //查询我的处理待办事项
                    if (loginUser != null) {
                        map.put("handlerUserSN", loginUser.getUserSN());
                        map.put("taskState", FlowTaskState.APPROVED.code);
                        map.put("groupDataSN", 1);
                        query.setQuery(map);
                    }
                } else {
                    if (loginUser != null) {
                        map.put("handlerUserSN", loginUser.getUserSN());
                        map.put("taskState", FlowTaskState.UNREAD.code);
                        query.setQuery(map);
                    }
                }
            }
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询用户全部代办事项", notes = "分页查询流程任务信息")
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public WebResult QueryAllByUserAction(){
        try{
            LoginUser loginUser = HsApp.getCurrent();
            var result = service.QueryAllByUserAction(loginUser.getUserSN(),FlowTaskState.UNREAD.code);
            if(result!=null){
                return  success("查询成功",result.size());
            }else {
                return success("查询成功",0);
            }
        }catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "处理待办事项", notes = "处理待办事项")
    @RequestMapping(value = "/handle", method = RequestMethod.POST)
    public WebResult handle(@RequestBody FlowDataHandleViewModel dataHandle) {
        try {
            var result = flowDataService.handle(dataHandle);
            if (result>0){
                return success("ok", result);
            }else{
                return success("no", 0);
            }

        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "获取流程数据信息", notes = "获取流程数据信息")
    @RequestMapping(value = "/dataInfo/{dataSN}", method = RequestMethod.GET)
    public WebResult getDataInfoByDataSn(@ApiParam(value = "数据编号") @PathVariable String dataSN) {
        return success("查询成功", service.getDataInfoByDataSn(dataSN));
    }

    @ApiOperation(value = "获取流程数据信息", notes = "获取流程数据信息")
    @RequestMapping(value = "/UserTask/{dataSN}", method = RequestMethod.GET)
    public WebResult getUserTaskByDataSn(@ApiParam(value = "数据编号") @PathVariable String dataSN) {
        var Current=HsApp.getCurrent();
        List<FlowUserTaskEntity> list = service.getUserTaskByObjectSN(dataSN,Current.getUserSN());
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (FlowUserTaskEntity entity : list) {
            var obj = entity.toMap();
            if (Current != null) {
                obj.put("HandlerUserName", Current.getUserName());
            } else {
                obj.put("HandlerUserName", "未知");
            }
            resultList.add(obj);
        }
        return success("查询成功", resultList);
    }

    @ApiOperation(value = "查询流程任务信息", notes = "根据流程任务信息编号查询流程任务信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存流程任务信息", notes = "查询/更新流程任务信息信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveFlowUserTask(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveFlowUserTask(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除流程任务信息信息", notes = "根据流程任务信息编号删除系流程任务信息信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "流程任务信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}